// 页面跳转
export function navigator(url, type) {
	switch(type) {
		case 'switchTab':
			uni.switchTab({
				url
			})
			break
		case 'reLaunch':
			uni.reLaunch({
				url
			})
			break
		case 'redirectTo':
			uni.redirectTo({
				url
			})
			break
		case 'switchTab':
			uni.switchTab({
				url
			})
			break
		default:
			uni.navigateTo({
				url
			})
	}
}

// 弹窗
export const Message = {
  modal(content, options = {}) {
    return new Promise((resolve, reject) => {
      let defaults = {
        title: '提示',
        confirmText: '确定',
        cancelText: '取消'
      }
      Object.assign(defaults, options);
      wx.showModal({
        // title: '提示',
        ...defaults,
        content,
        success(res) {
          if (res.confirm) {
            resolve(true);
          } else if (res.cancel) {
            resolve(false);
          }
        }
      })
    });
  },
  warn(content, options = {}) {
    let defaults = {
      title: '提示',
      confirmText: '确定'
    }
    Object.assign(defaults, options);
    return new Promise((resolve, reject) => {
      wx.showModal({
        ...defaults,
        content: content,
        showCancel: false,
        success() {
          resolve()
        }
      });
    });
  },
  warns(title) {
    wx.showToast({
      title,
      icon: 'none'
    })
  },
  success(title) {
    wx.showToast({
      title,
      icon: 'success'
    })
  },
  info(title) {
    wx.showToast({
      title,
      icon: 'loading',
      duration: 1000
    })
  },
  loading: {
    show(title = '') {
      wx.showLoading({
        mask: true,
        title,
      })
    },
    hide() {
      wx.hideLoading();
    }
  }
}

// 拨打电话
export function callPhone(phoneNumber) {
	uni.makePhoneCall({
		phoneNumber,
		fail(err) {
			console.log('makePhoneCall出错', err)
		}
	})
}

// 打开地图
export function openMap(latitude, longitude, name = '') {
	uni.openLocation({
		latitude: Number(latitude),
		longitude: Number(longitude),
		name,
		fail(err) {
			console.log('openLocation出错', err)
		}
	})
}

// 图片预览
export function previewImage(urls = [], current = 0) {
	uni.previewImage({
		urls: urls,
		current: current,
		indicator: 'default',
		loop: true,
		fail(err) {
			console.log('previewImage出错', urls, err)
		}
	})
}

// 复制文案
export function copyText(text, tips = '复制成功') {
	uni.setClipboardData({
		data: text,
		success(res) {
			uni.getClipboardData({
				success(res) {
					uni.showToast({
						title: tips,
						icon: 'none'
					})
				}
			})
		}
	})
}

// 版本更新方法
export function updateManager() {
	if (uni.canIUse('getUpdateManager')) {
		const updateManager = uni.getUpdateManager()
		updateManager.onCheckForUpdate(function(res) {
			if (res.hasUpdate) {
				updateManager.onUpdateReady(function() {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success: function(res) {
							if (res.confirm) {
								uni.clearStorage()
								updateManager.applyUpdate()
							}
						}
					})
				})
				updateManager.onUpdateFailed(function() {
					uni.showModal({
						title: '已经有新版本了哟~',
						content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
					})
				})
			}
		})
	} else {
		uni.showModal({
			title: '提示',
			content: '当前微信版本过低，无法直接进行版本更新，请升级到最新微信版本后重试。'
		})
	}
}
